package activity;


import java.util.LinkedList;
import java.util.List;

import utils.UIUtils;

import com.example.googlemarketdemo.MainActivity;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.Toast;
/**
 * 所有Activity的基类
 * @author Anson-pc
 *
 */
public abstract class BaseActivity extends ActionBarActivity {

	/**
	 * 应用的完全退出
	 * 显示最前端的Activity
	 * 退出提示
	 */
	/**
	 * 
	 */
	
	 
	private List<BaseActivity> activitys = new LinkedList<BaseActivity>();
	private BaseActivity mTopActivity;
	private long mPreClickTime;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		init();
		initView();
		initData();
		initListener();
	}
	
	@Override
	protected void onResume() {
		mTopActivity = this;
		activitys.add(this);
		super.onResume();
	}
	
	@Override
	protected void onDestroy() {
		activitys.remove(this);
		super.onDestroy();
	}

	protected void initListener() {
		// TODO Auto-generated method stub
		
	}

	protected void initData() {
		// TODO Auto-generated method stub
		
	}

	protected abstract void initView();

	protected void init() {
		// TODO Auto-generated method stub
		
	}
	
	
	/**
	 * 完全退出
	 */
	public void exit(){
		for (BaseActivity baseActivity : activitys) {
			baseActivity.finish();
		}
	}
	
	/**
	 * 点击返回的按键
	 */
	@Override
	public void onBackPressed() {
		if (this instanceof MainActivity) {//判断当前是否主页
			if (System.currentTimeMillis() - mPreClickTime >2000) {
				Toast.makeText(UIUtils.getContext(), "再按一次，退出谷歌电子市场", 0).show();
				mPreClickTime = System.currentTimeMillis();
				return;
			}else{
				exit();
			}
		}else{
			super.onBackPressed();
		}
	}
}
